package com.ruanmou.vip.rabbitmq.demo.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 欢迎同学们来到牧码人教育
 *
 * @Classname RabbitMQUtil
 * @Description TODO
 * @Date 2020-3-7 10:45
 * @Created by Gerry
 */
public class RabbitMQUtil {
    private static Connection connection;

    /**
     * 与rabbitmq建立连接
     * @return
     */
    public static Connection getConnection() {
        if (connection != null) {
            return connection;
        }

        ConnectionFactory factory = new ConnectionFactory();
        factory.setVirtualHost("/custer");
        factory.setHost("192.168.6.190"); // 用的是虚拟IP地址
        factory.setPort(5672);
        factory.setUsername("rabbit");
        factory.setPassword("rabbit");

        try {
            connection = factory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

        return connection;
    }

    public static void main(String[] args) {
        System.out.println(getConnection());
    }
}
